package com.java2007.hotel.dao.impl;

import com.java2007.hotel.dao.FoodTypeDao;
import com.java2007.hotel.pojo.FoodTable;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.jar.JarEntry;

/**
 * 菜系Dao实现类
 * @author csl
 * @version V1.0
 * @date 2021/01/22
 **/
public class FoodTypeDaoImpl  implements FoodTypeDao {
    /**
     * 增加一个菜系
     * @param foodType
     * @return
     */
    @Override
    public int addFoodType(FoodType foodType) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "INSERT INTO t_food_type\n" +
                "            (food_type_name,\n" +
                "             food_type_create_date,\n" +
                "             food_tyoe_update_date,\n" +
                "             food_type_creater_id)\n" +
                "VALUES (?,?,?,?)\n";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, foodType.getName(), foodType.getCreateDate()
                , foodType.getUpdateDate(), foodType.getCreaterId());
        return update;
    }

    /**
     *查询所有菜系
     * @return
     */
    @Override
    public List<FoodType> selectAllFoodType() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  food_type_id id,\n" +
                "  food_type_name name,\n" +
                "  food_type_create_date createDate,\n" +
                "  food_tyoe_update_date updateDate,\n" +
                "  food_type_creater_id createrId\n" +
                "FROM t_food_type";
        List<FoodType> query = queryRunner.query(sql, new BeanListHandler<FoodType>(FoodType.class));
        return query;
    }

    /**
     * 根据id修改菜系
     * @param updateDate
     * @param id
     * @return
     */
    @Override
    public int updateFoodType(String name, int id, Date updateDate) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "UPDATE t_food_type SET food_type_name= ?,food_tyoe_update_date = ? WHERE food_type_id =?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, name, updateDate, id);
        return update;
    }

    /**
     * 根据id删除菜系
     * @param id
     * @return
     */
    @Override
    public int deleteFoodTypeById(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "DELETE FROM t_food_type WHERE food_type_id = ?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, id);
        return update;
    }

    /**
     * 根据菜的名字查询菜系
     * @param name
     * @return
     */
    @Override
    public List<FoodType> selectFoodTypeByName(String name) throws SQLException {
        name = "%"+name+"%";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "                         food_type_id id,\n" +
                "                         food_type_name name,\n" +
                "                         food_type_create_date createDate,\n" +
                "                         food_tyoe_update_date updateDate,\n" +
                "                         food_type_creater_id createrId\n" +
                "                       FROM t_food_type\n" +
                "                       WHERE food_type_name LIKE ?";
        List<FoodType> query = queryRunner.query(sql, new BeanListHandler<FoodType>(FoodType.class), name);
        return query;
    }

    /**
     * 根据菜的名字查询一个菜系
     * @param name
     * @return
     */
    @Override
    public FoodType selectFoodTypeByNameOne(String name) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "                         food_type_id id,\n" +
                "                         food_type_name name,\n" +
                "                         food_type_create_date createDate,\n" +
                "                         food_tyoe_update_date updateDate,\n" +
                "                         food_type_creater_id createrId\n" +
                "                       FROM t_food_type\n" +
                "                       WHERE food_type_name = ?";
        FoodType query = queryRunner.query(sql, new BeanHandler<>(FoodType.class), name);
        return query;
    }

    /**
     * 根据id查找菜系
     * @param id
     * @return
     */
    @Override
    public FoodType selectFoodTypeById(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "                         food_type_id id,\n" +
                "                         food_type_name name,\n" +
                "                         food_type_create_date createDate,\n" +
                "                         food_tyoe_update_date updateDate,\n" +
                "                         food_type_creater_id createrId\n" +
                "                       FROM t_food_type\n" +
                "                       WHERE food_type_id = ?";
        FoodType query = queryRunner.query(sql, new BeanHandler<FoodType>(FoodType.class), id);
        return query;
    }
}
